package com.nbk.composition.controller

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.nbk.composition.bean.Response
import com.nbk.composition.dao.bean.Composition
import com.nbk.composition.dao.bean.Student
import com.nbk.composition.dao.bean.Teacher
import com.nbk.composition.dao.repository.CompositionMapper
import com.nbk.composition.dao.repository.OrderMapper
import com.nbk.composition.dao.repository.TeacherMapper
import com.nbk.composition.error
import com.nbk.composition.manager.TeacherAllot
import com.nbk.composition.utils.getCurrentTime
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

/**
 * Author:  LuoHaoLun
 * Email :  506503279@qq.com
 * Date  :  2021/6/9
 */
@RestController
@RequestMapping("/api/teacher/")
class TeacherController {

    @Autowired
    private lateinit var teacherMapper: TeacherMapper

    @Autowired
    private lateinit var compositionMapper: CompositionMapper

    @Autowired
    private lateinit var orderMapper: OrderMapper

    // 懒得加密了  登录
    @PostMapping("login")
    fun login(phone: String, password: String): Response {
        val query = KtQueryWrapper(Teacher::class.java).apply {
            eq(Teacher::phone, phone)
            eq(Teacher::password, password)
        }
        val teacher = teacherMapper.selectOne(query) ?: "用户名或密码错误".error()
        return Response().apply { data = teacher }
    }

    // 获取待批改作文
    @GetMapping("getWaitCorrect")
    fun getWaitCorrect(teacherId: Int): Response {
        val query = KtQueryWrapper(Composition::class.java).apply {
            eq(Composition::teacherId, teacherId)
            eq(Composition::state, "待批改")
        }.orderByDesc(Composition::id)
        val list = compositionMapper.selectList(query)
        return Response().apply { data = list }
    }

    // 获取已完成批改作文
    @GetMapping("getCompleteCorrect")
    fun getCompleteCorrect(teacherId: Int): Response {
        val query = KtQueryWrapper(Composition::class.java).apply {
            eq(Composition::teacherId, teacherId).and {
                it.eq(Composition::state, "待评价").or().eq(Composition::state, "已评价")
            }
        }.orderByDesc(Composition::id)
        val list = compositionMapper.selectList(query)
        return Response().apply { data = list }
    }

    // 获取待批改和已批改作文数量
    @GetMapping("getCorrectCount")
    fun getCorrectCount(teacherId: Int): Response {
        val query = KtQueryWrapper(Composition::class.java).apply {
            eq(Composition::teacherId, teacherId)
            eq(Composition::state, "待批改")
        }
        val wait = compositionMapper.selectCount(query)
        val query2 = KtQueryWrapper(Composition::class.java).apply {
            eq(Composition::teacherId, teacherId).and {
                it.eq(Composition::state, "待评价").or().eq(Composition::state, "已评价")
            }
        }
        val complete = compositionMapper.selectCount(query2)
        return Response().apply { data = listOf(wait, complete) }
    }

    // 提交批改或退回重写
    @PostMapping("commitCorrect")
    fun commitCorrect(@RequestBody composition: Composition): Response {
        // TODO 懒得做判断了
        if (composition.isAmendOrSendBack == true) {
            composition.residueCount = 1
        }
        composition.state = "待评价"
        composition.correctTime = getCurrentTime()
        compositionMapper.updateById(composition)
        TeacherAllot.remove(composition.teacherId)
        return Response()
    }
}